package com.vxdata.utils.modules.utils.security;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.core.util.CronExpression;

import java.text.ParseException;

/**
 * cron表达式转成中文
 */
public class CronUtils {

    private static final Integer SECOND = 0;
    private static final Integer MINUTE = 1;
    private static final Integer HOUR = 2;
    private static final Integer DAY = 3;
    private static final Integer MON = 4;
    private static final Integer WEEK = 5;
    private static final Integer YEAR = 6;

    private static final String WILDCARD_1 = "*";
    private static final String WILDCARD_2 = "-";
    private static final String WILDCARD_3 = "/";
    private static final String WILDCARD_4 = "?";
    private static final String WILDCARD_5 = "W";
    private static final String WILDCARD_6 = "L";
    private static final String WILDCARD_7 = "、";


    private static final Integer LENGTH = 6;

    public static String translateToChinese(String cronExp) {
        if (cronExp == null || cronExp.length() < 1) {
            return null;
//            return "cron表达式为空";
        }
        CronExpression exp;
        // 初始化cron表达式解析器
        try {
            exp = new CronExpression(cronExp);
        } catch (ParseException e) {
            return null;
//            return "corn表达式不正确";
        }
        String[] tmpCorns = cronExp.split(" ");
        StringBuffer sBuffer = new StringBuffer();
        StringBuffer sBuffer2 = new StringBuffer();
        if (tmpCorns.length >= LENGTH) {
            //解析年
//            if (tmpCorns[YEAR].contains(WILDCARD_2)) {
//                String[] split = tmpCorns[YEAR].split(WILDCARD_2);
//                sBuffer.append(split[0] + "年到" + split[1] + "年");
//            } else {
//                sBuffer.append(tmpCorns[YEAR]).append("年");
//            }
            //解析月
            if (tmpCorns[MON].equals(WILDCARD_1)) {
            } else if (tmpCorns[MON].contains(WILDCARD_2)) {
                String[] split = tmpCorns[MON].split(WILDCARD_2);
                sBuffer.append(split[0] + "月到" + split[1] + "月");
            } else {
                sBuffer.append(tmpCorns[MON]).append("月");
            }
            //解析周
            if (!tmpCorns[WEEK].equals(WILDCARD_1) && !tmpCorns[WEEK].equals(WILDCARD_4)) {
                if (tmpCorns[WEEK].contains(WILDCARD_2)) {
                    String[] split = tmpCorns[WEEK].split("-");
                    sBuffer.append("每周" + getWeek(split[0]) + "到周" + getWeek(split[1]));

                } else {
                    String[] split = tmpCorns[WEEK].split(",");
                    sBuffer.append("每周");
                    for (String tmp : split) {
                        sBuffer.append(getWeek(tmp) + WILDCARD_7);
                    }
                }

            }

            //解析日
            if (!tmpCorns[DAY].equals(WILDCARD_4)) {
                if (tmpCorns[DAY].equals(WILDCARD_1)) {
                    sBuffer.append("每天");
                } else if (tmpCorns[DAY].contains(WILDCARD_2)) {
                    String[] split = tmpCorns[DAY].split(WILDCARD_2);
                    sBuffer.append(split[0] + "号到" + split[1] + "号,");
                } else if (tmpCorns[DAY].contains(WILDCARD_3)) {
                    String[] split = tmpCorns[DAY].split(WILDCARD_3);
                    sBuffer.append(split[0] + "号开始每" + split[1] + "天的,");
                } else if (tmpCorns[DAY].contains(WILDCARD_5)) {
                    sBuffer.append(StringUtils.removeEnd(tmpCorns[DAY], WILDCARD_5) + "号最近的那个工作日");
                } else if (tmpCorns[DAY].equals(WILDCARD_6)) {
                    sBuffer.append("每月最后一天");
                } else {
                    sBuffer.append(tmpCorns[DAY] + "号");
                }
            }
            String s = StringUtils.removeEnd(sBuffer.toString(), WILDCARD_7) + "的";
            sBuffer2 = new StringBuffer(s);
            //间隔
            String interval = "";
            //解析时分秒
            for (int i = HOUR; i >= 0; i--) {
                String des = getHourStr(i);
                if (tmpCorns[i].equals(WILDCARD_1)) {
                    sBuffer2.append("每" + des);
                } else if (tmpCorns[i].contains(WILDCARD_2)) {
                    String[] split = tmpCorns[i].split(WILDCARD_2);
                    sBuffer2.append(split[0] + des + "到" + split[1] + des + ",");
                } else if (tmpCorns[i].contains(WILDCARD_3)) {
                    String[] split = tmpCorns[i].split(WILDCARD_3);
                    interval = interval + split[1] + des;
                    sBuffer2.append(split[0] + des);
                } else {
                    sBuffer2.append(tmpCorns[i] + des);
                }
            }
            if (StringUtils.isNotEmpty(interval)) {
                sBuffer2.append("开始,每隔").append(interval);
            }
        }
        return sBuffer2.toString();

    }

    public static String getWeek(String tmp) {
        switch (tmp) {
            case "MON":
                return ("1");
            case "TUE":
                return ("2");
            case "WED":
                return ("3");
            case "THU":
                return ("4");
            case "FRI":
                return ("5");
            case "SAT":
                return ("6");
            case "SUN":
                return ("7");
            default:
                return (tmp);
        }
    }

    private static String getHourStr(Integer param) {
        switch (param) {
            case 2:
                return ("时");
            case 1:
                return ("分");
            case 0:
                return ("秒");
            default:
                return (" ");
        }
    }

    //测试方法
    public static void main(String[] args) {
//        String CRON_EXPRESSION = "* * * * 6,7 3,4,7 2019-2020";
        String CRON_EXPRESSION = "0/3 7-8 * 5,7,23 * ?";
        System.out.println(CRON_EXPRESSION);
        System.out.println(CronUtils.translateToChinese(CRON_EXPRESSION));


    }
}